package bingliandshuju;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zhzz.SingleDataofzhzz;

import com.example.animaldoctor03.AllIllness;
import com.example.animaldoctor03.BingLi2;
import com.example.animaldoctor03.JBXQ;
import com.example.animaldoctor03.R;
import com.example.animaldoctor03.YiXuanZhengZhuang;
import com.example.animaldoctor03.ZhengDuanFF;
import com.example.animaldoctor03.ZhengZhuangFZ;

import commonClass.ADAQueryClinicalSymptomsKeyByDiseaseIDAction;
import commonClass.FunctionDispatcher;
import commonClass.RemoteActionsName;
import commonClass.FunctionDispatcher.IRequestCompeletListener;
import Modle.disease.ClinicalSymptomsKey;
import Modle.disease.Symptoms;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BingLi3 extends Activity {

	private String nameofBL3 = "";
	private String GLofBL3;
	private TextView textofBL3 = null;
	private ListView listviewofbingli3 = null;
	private Button buttonofbingli3toJBXQ = null,
			buttonofbingli3forallchoice = null;
	private Button buttonofbingli3toagain = null,buttonofBL3F = null;

	private Symptoms symptomsofBL3getinformation = null;
	private List<Object> listofBL3getinformation = new ArrayList<Object>();
	private String animalIDofBL3 = "";
	private ArrayList<Object> resultListofBL3 = null;

	private void initElemofBL3() {

		setContentView(R.layout.bingli3);
		buttonofBL3F = (Button) findViewById(R.id.buttonofBLF3);
		textofBL3 = (TextView) findViewById(R.id.textviewofbinliof3);
		listviewofbingli3 = (ListView) findViewById(R.id.listofbingli3);
		buttonofbingli3forallchoice = (Button) findViewById(R.id.buttonofbingli34);
		buttonofbingli3toJBXQ = (Button) findViewById(R.id.buttonofbingli31);
		buttonofbingli3toagain = (Button) findViewById(R.id.buttonofbingli32);

		buttonofbingli3toagain
				.setOnClickListener(new listenerofbingli3toagain());
		buttonofbingli3toJBXQ.setOnClickListener(new listenetofbingli3toJBXQ());
		
				
		buttonofBL3F.setOnClickListener(new listenerofBL3F());

	}

	private void getInformationofBL3() {

		SingleDataofAll singleDataofBL3 = SingleDataofAll.getInstance();
		symptomsofBL3getinformation = singleDataofBL3.getObject();
		listofBL3getinformation = singleDataofBL3.getListinformation();
	}

	private void getInformationfromupofBL3() {
		Intent intentofBL3 = getIntent();
		Bundle bundleofBL3 = intentofBL3.getExtras();
		ClinicalSymptomsKey CofBL3 = (ClinicalSymptomsKey) bundleofBL3
				.getSerializable("shuju");
		animalIDofBL3 = CofBL3.getDiseaseID();
		nameofBL3 = CofBL3.getDiseaseName();
		DecimalFormat format = new DecimalFormat("#.0000");

		GLofBL3 = format.format(CofBL3.getDiseaseProbability());

		Message msgofBL3 = new Message();
		msgofBL3.what = 1;
		handleofBL3.sendMessage(msgofBL3);

	}

	private ArrayList<Object> assemblyofBL3() {

		getInformationfromupofBL3();
		ArrayList<Object> list = new ArrayList<Object>();

		list.add(animalIDofBL3);
		// Integer i = new Integer(10);
		// list.add(i);

		return list;
	}

	private void bindingofBL3(final ArrayList<Object> list) {
		try {

			getInformationofBL3();
			List<String> listofoldIdsofBL3 = new ArrayList<String>();
			listofoldIdsofBL3.add(symptomsofBL3getinformation.getSymptomsID());
			for (int i = 0; i < listofBL3getinformation.size(); i++) {

				ClinicalSymptomsKey ctextofBL3 = (ClinicalSymptomsKey) listofBL3getinformation
						.get(i);
				listofoldIdsofBL3.add(ctextofBL3.getClinicalSymptomsKeyID());

			}

			List<Object> listofZZhuanofBL3 = new ArrayList<Object>();

			List<Object> listofBL3QC = new ArrayList<Object>();

			for (int j = 0; j < list.size(); j++) {

				ClinicalSymptomsKey cetext1ofBL3 = (ClinicalSymptomsKey) list
						.get(j);
				String sofctext1ofBL3 = cetext1ofBL3.getClinicalSymptomsKeyID();
				for (String s : listofoldIdsofBL3) {
					if (sofctext1ofBL3.equals(s)) {

						listofBL3QC.add(cetext1ofBL3);
					}

				}
			}

			for (int i = 0; i < list.size(); i++) {

				int jishuofBL3 = 0;
				ClinicalSymptomsKey ctext2ofBL3 = (ClinicalSymptomsKey) list
						.get(i);
				String sofctext2ofBL3 = ctext2ofBL3.getClinicalSymptomsKeyID();
				for (String s : listofoldIdsofBL3) {
					if (!sofctext2ofBL3.equals(s)) {
						jishuofBL3++;

					}
					if (jishuofBL3 == listofoldIdsofBL3.size()) {
						listofZZhuanofBL3.add(ctext2ofBL3);
					}

				}
			}

			String[] resultofBL3 = new String[listofZZhuanofBL3.size()];
			// list.toArray(new String[list.size()]);
			for (int i = 0; i < listofZZhuanofBL3.size(); i++) {

				resultofBL3[i] = ((ClinicalSymptomsKey) listofZZhuanofBL3
						.get(i)).getClinicalSymptomsKey();
			}
			ArrayAdapter<String> arrayAdapterofBL3 = new ArrayAdapter<>(this,
					android.R.layout.simple_list_item_multiple_choice,
					resultofBL3);
			listviewofbingli3.setAdapter(arrayAdapterofBL3);

			buttonofbingli3forallchoice.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					ArrayList<Object> listofnextofBL3 = new ArrayList<Object>();
					for (long ids : listviewofbingli3.getCheckItemIds()) {

						int i = (int) ids;

						listofnextofBL3.add((ClinicalSymptomsKey) list.get(i));
					}

					// 把数据储存好
					SingleDataofAll singleDataofAllofBL3 = SingleDataofAll
							.getInstance();
					singleDataofAllofBL3.setListinformation(listofnextofBL3);
					
					try {
						Intent intentofbingli3toallchoice = new Intent(BingLi3.this,
								YiXuanZhengZhuang.class);
						startActivity(intentofbingli3toallchoice);
						finish();
					} catch (RuntimeException e) {
						android.app.AlertDialog.Builder builderofBL3oftoYXZZ = new AlertDialog.Builder(
								BingLi3.this);
						builderofBL3oftoYXZZ.setMessage("不能使用该功能, 请重试或者使用其他功能");
						builderofBL3oftoYXZZ.create().show();
					}
				}
			});
		} catch (NullPointerException e) {
			android.app.AlertDialog.Builder builderofBL3 = new AlertDialog.Builder(
					BingLi3.this);
			builderofBL3.setMessage("无数据, 请用其他功能");
			builderofBL3.create().show();
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		initElemofBL3();
		ArrayList<Object> parasListofBL3 = assemblyofBL3();

		final ArrayList<Object> resultListTofBL3 = null;

		ADAQueryClinicalSymptomsKeyByDiseaseIDAction iActionofBL3 = new ADAQueryClinicalSymptomsKeyByDiseaseIDAction();
		FunctionDispatcher functionDispacherofBL3 = new FunctionDispatcher();

		functionDispacherofBL3.exceute(
				RemoteActionsName.QueryClinicalSymptomsKeyByDiseaseIDAction,
				parasListofBL3, iActionofBL3, new IRequestCompeletListener() {

					@Override
					public void doCompelet(ArrayList<Object> resultList) {
						// TODO Auto-generated method stub
						try {
							Log.v("ZhengZhuangFZ2",
									"post:" + resultList.toString());
							BingLi3.this.resultListofBL3 = resultList;
							bindingofBL3(resultList);
						} catch (NullPointerException e) {
							android.app.AlertDialog.Builder builderofBL3off = new AlertDialog.Builder(
									BingLi3.this);
							builderofBL3off.setMessage("无返回值 , 请用其他功能");
							builderofBL3off.create().show();
						}
					}
				}, this);

	}

	public class listenerofBL3F implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				SingleDataofAll sAll = SingleDataofAll.getInstance();
				sAll.removeAll();
				SingleInformationofIllnessDetail sInfor = SingleInformationofIllnessDetail
						.getInstance();
				sInfor.removeAll();
				SingleDataofzhzz sDofzh = SingleDataofzhzz.getInstance();
				sDofzh.removeAll();
				Intent intentofBL3F = new Intent(BingLi3.this,
						ZhengDuanFF.class);
				startActivity(intentofBL3F);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL3ofF = new AlertDialog.Builder(
						BingLi3.this);
				builderofBL3ofF.setMessage("不能使用该功能, 请重试或者使用其他功能");
				builderofBL3ofF.create().show();
			}

		}

	}

	class listenerofbingli3toagain implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				List<Object> listofagainofBL3 = new ArrayList<Object>();
				SingleDataofAll singDataofallofBL3 = SingleDataofAll
						.getInstance();
				listofagainofBL3 = singDataofallofBL3.getListinformation();
				Bundle bundleofagainofBL3 = new Bundle();
				bundleofagainofBL3.putSerializable("shuju",
						(Serializable) listofagainofBL3);
				Intent intentofbingli3toagain = new Intent(BingLi3.this,
						AllIllness.class);
				intentofbingli3toagain.putExtras(bundleofagainofBL3);
				startActivity(intentofbingli3toagain);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL3oftoA = new AlertDialog.Builder(
						BingLi3.this);
				builderofBL3oftoA.setMessage("不能使用该功能, 请重试或者使用其他功能");
				builderofBL3oftoA.create().show();
			}
		}

	}


	class listenetofbingli3toJBXQ implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				Bundle bundleofBL3 = new Bundle();
				bundleofBL3.putString("shuju", animalIDofBL3);
				Intent intentofbingli3toJBXQ = new Intent(BingLi3.this,
						JBXQ.class);
				intentofbingli3toJBXQ.putExtras(bundleofBL3);
				startActivity(intentofbingli3toJBXQ);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL3oftoJBXQ = new AlertDialog.Builder(
						BingLi3.this);
				builderofBL3oftoJBXQ.setMessage("不能使用该功能, 请重试或者使用其他功能");
				builderofBL3oftoJBXQ.create().show();
			}
		}

	}

	public Handler handleofBL3 = new Handler() {

		public void handleMessage(Message msg) {

			if (msg.what == 1) {
				textofBL3.setText("病名：" + nameofBL3 + " " + "概率：" + "0"
						+ GLofBL3);
			}
		}
	};
}
